package cn.jietuo.helloworld;

import cn.jietuo.util.RabbitConstant;
import cn.jietuo.util.RabbitUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2021/2/15
 * @description: 生产者
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 获取TCP长连接
        Connection connection = RabbitUtil.getConnection();
        // 创建通信通道，相当于TCP中的虚拟连接
        Channel channel = connection.createChannel();
        // 创建队列
        /**
         * @param queue the name of the queue
         *             队列名称ID
         * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
         *                是否持久化数据，false对应不持久化，MQ停掉数据救护丢失
         * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
         *                  是否队列私有化，false代表所有消费者都可以访问，true代表只有第一次拥有它的消费者才能一直使用它，其他消费者不让访问
         * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
         *                   是否自动删除，false代表连接停掉之后不自动删除这个队列
         * @param arguments other properties (construction arguments) for the queue
         *                  其他额外的参数
         */
        channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORLD,false,false,false,null);

        // 准备要发送的消息
        String message = "你好，RabbitMQ";
        // 发布消息
        /**
         * @param exchange the exchange to publish the message to
         *                 交换机
         * @param routingKey the routing key
         *                   路由key
         * @param mandatory true if the 'mandatory' flag is to be set
         * @param immediate true if the 'immediate' flag is to be set. Note that the RabbitMQ server does not support this flag.
         * @param props other properties for the message - routing headers etc
         *              其他额外的参数
         * @param body the message body
         *             消息字节数组
         */
        channel.basicPublish("",RabbitConstant.QUEUE_HELLOWORLD,false,false,null,message.getBytes());
        channel.close();
        connection.close();
        System.out.println("发送消息成功");
    }
}
